package com.orangelife.service.propFeeInvoiceLink.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeInvoiceLink.IPropFeeInvoiceLinkService;
import com.orangelife.service.propFeeInvoiceLink.model.PropFeeInvoiceLinkDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeInvoiceLink类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-20 21:24:06
 * ==============================================================
 */
@Service("propFeeInvoiceLinkService")
public class PropFeeInvoiceLinkServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeInvoiceLinkService {
	private static Logger logger = Logger
			.getLogger(PropFeeInvoiceLinkServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeInvoiceLinkServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeInvoiceLink(PropFeeInvoiceLinkDto propFeeInvoiceLink)
			throws AppBizException {

		Assert.notNull(propFeeInvoiceLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeInvoiceLink.getType())) {
			if (propFeeInvoiceLink.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		propFeeInvoiceLink.setInvoiceLinkID(IdUtil.getuuId());
		propFeeInvoiceLink.setItemStat(TypeConst.FLAG_ON);
		propFeeInvoiceLink.setTscreate(new Date());

		return this.getSqlSession().insert(
				"PropFeeInvoiceLinkMapper.insertSelective", propFeeInvoiceLink);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeInvoiceLink(PropFeeInvoiceLinkDto propFeeInvoiceLink)
			throws AppBizException {

		Assert.notNull(propFeeInvoiceLink);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeInvoiceLink.getInvoiceLinkID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeInvoiceLinkDto oldPropFeeInvoiceLink = this
				.findPropFeeInvoiceLinkByID(propFeeInvoiceLink
						.getInvoiceLinkID());

		if (!StringUtils.isBlank(propFeeInvoiceLink.getType())) {
			if (propFeeInvoiceLink.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (oldPropFeeInvoiceLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		return this.getSqlSession().update(
				"PropFeeInvoiceLinkMapper.updateByPrimaryKeySelective",
				propFeeInvoiceLink);
	}

	@Override
	public PropFeeInvoiceLinkDto findPropFeeInvoiceLinkByID(
			String propFeeInvoiceLinkID) throws AppBizException {
		if (StringUtils.isBlank(propFeeInvoiceLinkID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeInvoiceLinkDto oldPropFeeInvoiceLink = this.getSqlSession()
				.selectOne("PropFeeInvoiceLinkSearchMapper.selectByPrimaryKey",
						propFeeInvoiceLinkID);
		if (oldPropFeeInvoiceLink == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeInvoiceLink.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeInvoiceLink;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeInvoiceLinkByLogic(String propFeeInvoiceLinkID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeInvoiceLinkID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeInvoiceLinkDto oldPropFeeInvoiceLink = this
				.findPropFeeInvoiceLinkByID(propFeeInvoiceLinkID);

		oldPropFeeInvoiceLink.setItemStat(TypeConst.FLAG_OFF);

		return this.getSqlSession().update(
				"PropFeeInvoiceLinkMapper.updateByPrimaryKeySelective",
				oldPropFeeInvoiceLink);
	}

	@Override
	public List<Map<String, Object>> findPropFeeInvoiceLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeInvoiceLinkSearchMapper.findAllPropFeeInvoiceLink",
						map);
		return list;
	}

	@Override
	public List<PropFeeInvoiceLinkDto> getPropFeeInvoiceLinkByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeInvoiceLinkDto> list = this.getSqlSession().selectList(
				"PropFeeInvoiceLinkSearchMapper.getAllPropFeeInvoiceLinkDto",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeInvoiceLinkByRelevantID(
			String invoiceLinkID, String invoiceID, String linkID, String type)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("invoiceLinkID", invoiceLinkID);
		map.put("invoiceID", invoiceID);
		map.put("linkID", linkID);
		map.put("type", type);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeInvoiceLinkSearchMapper.selectPropFeeInvoiceLinkDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeInvoiceLinkDto> findPropFeeInvoiceLinkByRelevantID(
			String invoiceLinkID, String invoiceID, String linkID, String type)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("invoiceLinkID", invoiceLinkID);
		map.put("invoiceID", invoiceID);
		map.put("linkID", linkID);
		map.put("type", type);

		List<PropFeeInvoiceLinkDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeInvoiceLinkSearchMapper.findPropFeeInvoiceLinkDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeInvoiceLinkDto> bauthfindPropFeeInvoiceLink(
			List<String> propFeeInvoiceLinkIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeInvoiceLinkDto> list = this.getSqlSession().selectList(
				"PropFeeInvoiceLinkSearchMapper.batchFindPropFeeInvoiceLink",
				propFeeInvoiceLinkIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeInvoiceLinkByMap(
			Map<String, Object> propFeeInvoiceLink) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeInvoiceLinkSearchMapper.batchFindPropFeeInvoiceLinkByMap",
						propFeeInvoiceLink);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public int addPropFeeInvoiceLink(
			List<PropFeeInvoiceLinkDto> propFeeReceiptLinkDtos)
			throws AppBizException {
		if (propFeeReceiptLinkDtos != null && propFeeReceiptLinkDtos.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeInvoiceLinkBauthMapper.bauthInsert",
					propFeeReceiptLinkDtos);
		}
		return 1;
	}

}